#!/usr/bin/python
# -*- coding: utf-8 -*-

# Simplified BSD License (BSD)
# 
# Copyright (c) 2013, dr.kohlone@gmail.com
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
# 
# * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# 
# * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from __future__ import unicode_literals
import pymongo

# pylint: disable-msg=R0904

# mongo wrapper class imposing safe write (see "write concern")
# change w value to change the number of write to wait for
class SafeMongoClient(pymongo.MongoClient):
    def __init__(self, host=None, port=None):
        pymongo.MongoClient.__init__(self, host, port, w=1, j=False)
        
    def close(self):
        try:
            pymongo.MongoClient.close(self)
        except:
            pass
        
#TODO: complete these methods
class MongoTools(object):
    @staticmethod
    def delete_collection(database_name, collection_name):
        pass
    
    @staticmethod
    def delete_database(database_name):
        pass

# wrapper class for mongorestore functionality
class MongoRestore(object):
    @staticmethod
    def restore(database, collection, dumpfile, host=None, port=None, user=None, password=None):
        request = 'pymongo'
        if database:
            request += ' --db %s' % (database)
        if collection:
            request += ' --collection %s' % (collection)
        if host:
            request += ' --host %s' % (host)
        if dumpfile:
            pass
            
#         h = pymongo.MongoClient.HOST
#         p = pymongo.MongoClient.PORT
#         if host:
#             h = host
#         if port:
#             p = port
        
#         mongorestore --collection people --db accounts dump/accounts/people.bson
#             
#         os.system('notepad.exe "%s" &' % (module_full))
#         
#         mongorestore --host mongodb1.example.net --port 37017 --username user --password pass /opt/backup/mongodump-2011-10-24

def connect(host=None, port=None):
    conn = SafeMongoClient(host, port)
    return conn

def search(connection, base, collec, dico):
    return connection[base][collec].find(dico)

def empty_collection(connection, base, collection):
    connection[base][collection].drop()
    

         
